#include "engine.h"
#include "stringhash.h"

#ifdef DEBUG_STRINGHASH

string* nullstr=NULL;
struct stringhashchecker_t
{
	struct checkerelem_t
	{
		uint32 key;
		string str;

		uint32 gethashkey() const
		{
			return key;
		}

		checkerelem_t* nextinhashmap;
	};

	uint32 elemnum;
	checkerelem_t elembuf[32768];
	hashmap_t<checkerelem_t,16384> hashmap;

	stringhashchecker_t()
	{
		elemnum=0;
		nullstr=insert(0,"");
	}


	string* insert(uint32 key, const char* str)
	{
		ASSERT(elemnum<32768);
		checkerelem_t* e=hashmap.get(key);
		ASSERT(!e || e->str==str);
		if (!e)
		{
			elembuf[elemnum].key=key;
			elembuf[elemnum].str=str;
			hashmap.insert(elembuf+elemnum);
			++elemnum;
			return &elembuf[elemnum-1].str;
		}
		else
		{
			return &e->str;
		}
	}

	const char* getstr(uint32 key)
	{
		return hashmap.get(key)->str.c_str();
	}

	static stringhashchecker_t& get()
	{
		static stringhashchecker_t shch;
		return shch;
	}
} ;
const char* gethashstr(uint32 key)
{
	return stringhashchecker_t::get().getstr(key);
}
string* inserthashstring(uint32 key,const char* c)
{
	return stringhashchecker_t::get().insert(key,c);
}
#endif
